﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SerialTool
{
    public partial class FormValCal : Form
    {
        delegate void ShowFrameHandler(Frame frame);


        bool hasSymbol = false;
        int byteNum = 1;
        bool littleEndian = true;

        public FormValCal()
        {
            InitializeComponent();
        }

        public void CalFrame(Frame frame)
        {

            if (this.InvokeRequired)
            {
                this.Invoke(new ShowFrameHandler(CalFrame), new object[] { frame });
            }
            else
            {
                byte[] data = frame.RawData;

                byte[] tmp=new byte[12];
                Decimal dec = 0;

                int startIndex = (int)numericUpDown1.Value;

                if (littleEndian == false)
                {
                    for (int i = 0; i < byteNum; i++)
                    {
                        tmp[i] = data[startIndex + byteNum - 1 - i];
                    }
                }
                else
                {
                    for (int i = 0; i < byteNum; i++)
                    {
                        tmp[i] = data[startIndex + i];
                    }
                }

                if (hasSymbol)
                {
                    switch (byteNum)
                    {
                        case 1:
                            dec = (sbyte)tmp[0];
                            break;
                        case 2:
                            dec = BitConverter.ToInt16(tmp,0);
                            break;
                        case 4:
                            dec = BitConverter.ToInt32(tmp,0);
                            break;
                    }
                }
                else
                {
                    switch (byteNum)
                    {
                        case 1:
                            dec = tmp[0];
                            break;
                        case 2:
                            dec = BitConverter.ToUInt16(tmp,0);
                            break;
                        case 4:
                            dec = BitConverter.ToUInt32(tmp,0);
                            break;
                    }
                }


                textBox1.Text = dec.ToString();
                


            }


        }

        private void rbUINT8_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            string tag = rb.Tag.ToString();
            string[] paramList = tag.Split(new char[] { ',' });
            hasSymbol = paramList[0] == "0" ? false : true;
            byteNum = int.Parse(paramList[1]);


        }

        private void rbLittle_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            string tag = rb.Tag.ToString();
            littleEndian = tag == "0" ? true : false;
        }
    }
}
